package com.example.demo.service.impl;

import com.example.demo.mapper.QuestionMapper;
import com.example.demo.model.Question;
import com.example.demo.model.TestRecord;
import com.example.demo.service.MbtiCalculationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * MBTI计算服务实现类
 */
@Service
public class MbtiCalculationServiceImpl implements MbtiCalculationService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public TestRecord calculateResult(Map<Integer, String> answers) {
        // 计算各维度得分
        Map<String, Integer> scores = calculateDimensionScores(answers);
        
        // 确定MBTI类型
        String mbtiType = determineMbtiType(scores);
        
        // 创建测试记录对象
        TestRecord testRecord = new TestRecord();
        testRecord.setEScore(scores.get("E"));
        testRecord.setIScore(scores.get("I"));
        testRecord.setSScore(scores.get("S"));
        testRecord.setNScore(scores.get("N"));
        testRecord.setTScore(scores.get("T"));
        testRecord.setFScore(scores.get("F"));
        testRecord.setJScore(scores.get("J"));
        testRecord.setPScore(scores.get("P"));
        testRecord.setPersonalityType(mbtiType);
        
        return testRecord;
    }

    @Override
    public Map<String, Integer> calculateDimensionScores(Map<Integer, String> answers) {
        // 初始化各维度得分
        Map<String, Integer> scores = new HashMap<>();
        scores.put("E", 0);
        scores.put("I", 0);
        scores.put("S", 0);
        scores.put("N", 0);
        scores.put("T", 0);
        scores.put("F", 0);
        scores.put("J", 0);
        scores.put("P", 0);
        
        // 遍历每个答题结果
        for (Map.Entry<Integer, String> entry : answers.entrySet()) {
            Integer questionId = entry.getKey();
            String chosenOption = entry.getValue();
            
            // 获取题目信息
            Question question = questionMapper.findById(questionId);
            if (question == null) {
                continue;
            }
            
            // 根据选择的选项增加相应维度的得分
            if ("A".equals(chosenOption)) {
                String dimension = question.getAffectsA();
                scores.put(dimension, scores.get(dimension) + 1);
            } else if ("B".equals(chosenOption)) {
                String dimension = question.getAffectsB();
                scores.put(dimension, scores.get(dimension) + 1);
            }
        }
        
        return scores;
    }

    @Override
    public String determineMbtiType(Map<String, Integer> scores) {
        // 根据各维度得分确定MBTI类型
        StringBuilder mbtiType = new StringBuilder();
        
        // E/I维度
        if (scores.get("E") >= scores.get("I")) {
            mbtiType.append("E");
        } else {
            mbtiType.append("I");
        }
        
        // S/N维度
        if (scores.get("S") >= scores.get("N")) {
            mbtiType.append("S");
        } else {
            mbtiType.append("N");
        }
        
        // T/F维度
        if (scores.get("T") >= scores.get("F")) {
            mbtiType.append("T");
        } else {
            mbtiType.append("F");
        }
        
        // J/P维度
        if (scores.get("J") >= scores.get("P")) {
            mbtiType.append("J");
        } else {
            mbtiType.append("P");
        }
        
        return mbtiType.toString();
    }
} 